package middleware

import (
	"encoding/json"
	"github.com/kataras/iris/v12"
	"iris_product/common"
	"iris_product/datamodels"
	"iris_product/encrypt"
	"log"
)

func AuthConProduct(ctx iris.Context) {
	// 验证
	userSign := ctx.GetCookie("sign")
	// 解密
	userByge,err :=encrypt.DePwdCode(userSign)
	if err != nil {
		common.Log.Error("解密失败或秘钥被篡改:" + err.Error())
		ctx.Redirect("/user/login")
		return
	}
	log.Println("解密后的字符为: " + string(userByge))
	user := &datamodels.User{}
	err = json.Unmarshal(userByge,user)
	if err != nil {
		common.Log.Error("解密后的字符解析失败:" + err.Error())
		ctx.Redirect("/user/login")
		return
	}

	ctx.Next()
}